package com.wpen.unit.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpen.unit.cvt.system.MenuCvt;
import com.wpen.unit.infra.mybatis.system.po.MenuPo;
import com.wpen.unit.infra.repository.system.MenuRepository;
import com.wpen.unit.service.system.MenuService;
import com.wpen.unit.web.dto.system.MenuListRequest;
import com.wpen.unit.web.dto.system.MenuListResponse;
import com.wpen.unit.web.dto.system.MenuListRowsResponse;
import com.wpen.unit.web.dto.system.MenuTreeRequest;
import com.wpen.unit.web.dto.system.MenuTreeResponse;
import com.wpen.unit.web.dto.system.MenuTreeRowResponse;

/**
 * 菜单业务处理.
 * 
 * @author Wang Peng
 * @date 2022-12-22
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuRepository menuRepository;

    @Override
    public MenuListResponse queryListMenu(MenuListRequest menuListRequest) {
        int index = menuListRequest.getIndex();
        int size = menuListRequest.getSize();
        // 数据库查询
        MenuPo menuPo = MenuCvt.INSTANCE.queryListDto2Do(menuListRequest);

        long total = menuRepository.count(menuPo);
        List<MenuPo> menuPoLists = menuRepository.queryList(index, size, menuPo);

        // 返回响应信息
        MenuListResponse menuListResponse = new MenuListResponse();
        menuListResponse.setTotal(total);
        menuListResponse.setCount(0);
        if (menuPoLists != null) {
            menuListResponse.setCount(menuPoLists.size());
            List<MenuListRowsResponse> listRows = menuListResponse.getRows();
            for (MenuPo inMenuPo : menuPoLists) {
                MenuListRowsResponse row = MenuCvt.INSTANCE.queryListRowDo2Dto(inMenuPo);
                listRows.add(row);
            }
            menuListResponse.setRows(listRows);
        }

        return menuListResponse;
    }

    @Override
    public MenuTreeResponse menuTree(MenuTreeRequest menuTreeParam) {
        List<MenuPo> sysMenuDb = menuRepository.queryListOrder();
        String pid = "0";
        // 组个json
        List<MenuTreeRowResponse> listMenus = makeMenus(sysMenuDb, pid);

        // 返回响应信息
        MenuTreeResponse menuTree_S = new MenuTreeResponse();
        menuTree_S.setList(listMenus);
        return menuTree_S;
    }

    private List<MenuTreeRowResponse> makeMenus(List<MenuPo> menus, String pid) {
        if (menus == null || menus.isEmpty()) {
            return new ArrayList<>();
        }
        List<MenuPo> list0 = new ArrayList<>();
        Map<String, List<MenuPo>> mapALLs = new HashMap<>();
        for (MenuPo sysMenu : menus) {
            if (pid.equals(sysMenu.getMenuPid())) {
                list0.add(sysMenu);
            } else {
                if (mapALLs.containsKey(sysMenu.getMenuPid())) {
                    mapALLs.get(sysMenu.getMenuPid()).add(sysMenu);
                } else {
                    List<MenuPo> listone = new ArrayList<>();
                    listone.add(sysMenu);
                    mapALLs.put(sysMenu.getMenuPid(), listone);
                }
            }
        }

        List<MenuTreeRowResponse> backMaps = new ArrayList<>(list0.size());
        for (MenuPo sysMenu2 : list0) {
            MenuTreeRowResponse backOne = makeMenuLoops(sysMenu2, mapALLs, 1);
            backMaps.add(backOne);
        }
        return backMaps;
    }

    private MenuTreeRowResponse makeMenuLoops(MenuPo sysMenu, Map<String, List<MenuPo>> map, int deep) {
        MenuTreeRowResponse rowResult = new MenuTreeRowResponse();
        rowResult.setIcon(sysMenu.getMenuImage());
        rowResult.setMenuId(sysMenu.getMenuId());
        rowResult.setName(sysMenu.getMenuName());
        rowResult.setOrderNum(sysMenu.getOrders());
        rowResult.setParentId(sysMenu.getMenuPid());
        rowResult.setUrl(sysMenu.getMenuUrl());
        if (map.containsKey(sysMenu.getMenuId())) {
            List<MenuPo> childrens = map.get(sysMenu.getMenuId());
            List<MenuTreeRowResponse> backMaps = new ArrayList<>(childrens.size());
            for (MenuPo sysMenu2 : childrens) {
                MenuTreeRowResponse back = makeMenuLoops(sysMenu2, map, deep + 1);
                backMaps.add(back);
            }
            rowResult.getList().addAll(backMaps);
        }
        return rowResult;
    }

}
